package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum UpdateResult implements EnumTrans<CompleteEntity.UpdateResult, UpdateResult> {
    /**
     * DEFAULT_TYPE
     * SUCCESS 更新成功
     * FAIL 更新失败，其他异常
     * UPDATING 正在更新
     * VERSION_NOT_MATCH 版本不匹配
     */
    DEFAULT_TYPE(CompleteEntity.UpdateResult.DEFAULT_TYPE),
    SUCCESS(CompleteEntity.UpdateResult.SUCCESS),
    FAIL(CompleteEntity.UpdateResult.FAIL),
    UPDATING(CompleteEntity.UpdateResult.UPDATING),
    VERSION_NOT_MATCH(CompleteEntity.UpdateResult.VERSION_NOT_MATCH),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.UpdateResult updateResult;
    private static final long serialVersionUID = 987654321987654321L;

    UpdateResult(CompleteEntity.UpdateResult updateResult) {
        this.id = updateResult.getNumber();
        this.name = updateResult.name();
        this.updateResult = updateResult;
    }

    /**
     * @param updateResult
     * @return 对应的类型
     */
    public static UpdateResult getUpdateResult(CompleteEntity.UpdateResult updateResult) {
        Objects.requireNonNull(updateResult, "updateResult can not be null.");
        return Arrays.stream(UpdateResult.values())
                .filter(e -> e.name.equals(updateResult.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.UpdateResult toProto() {
        return this.updateResult;
    }

    @Override
    public UpdateResult formProto(CompleteEntity.UpdateResult updateResult) {
        return getUpdateResult(updateResult);
    }
}

